package com.suanfa.collect;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

public class PhoneNumber {

    char[][] chars = {{},{},{'a','b','c'},{'d','e','f'},{'g','h','i'},{'j','k','l'}
            ,{'m','n','o'},{'p','q','r','s'},{'t','u','r'},{'w','x','y','z'}};

    public static void main(String[] args) {
        new PhoneNumber().letterCombinations("23");
    }

    public List<String> letterCombinations(String digits) {
        StringBuilder stringBuilder = new StringBuilder("");
        List<String> list = new ArrayList<>();
        char[] chars = digits.toCharArray();
        getChars(chars, 0, stringBuilder, list);
        return list;
    }

    public void getChars(char[] nums, int index, StringBuilder stringBuilder, List<String> list){
        if(index == nums.length){
            if(stringBuilder.length() > 0){
                list.add(new String(stringBuilder.toString()));
            }
            return;
        }
        int num = Character.getNumericValue(nums[index]);
        char[] ch = chars[num];
        for(int i=0; i<ch.length; i++){
            stringBuilder.append(ch[i]);
            getChars(nums, index+1, stringBuilder, list);
            stringBuilder.deleteCharAt(stringBuilder.length()-1);
        }
    }
}
